package com.fanhao.dllo.justforyou.fragment;

import android.content.Intent;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.activity.FoodHouseActivity;
import com.fanhao.dllo.justforyou.activity.FoodShowActivity;
import com.fanhao.dllo.justforyou.adapter.AfficheViewPagerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class FoodFragment extends Fragment implements
        View.OnClickListener, ViewPager.OnPageChangeListener {

    private ViewPager viewPager;
    private AfficheViewPagerAdapter pagerAdapter;
    private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    //图片的数组
    private int []image = {R.mipmap.topfood, R.mipmap.topfood2, R.mipmap.topfood3,
            R.mipmap.topfood4};

    private ImageView[] image_small = new ImageView[4];

    //文字的数组
//private String [] text = {"广大狼友们请注意:" + "/n" + "    软件将在今晚十一点更新,给狼友们带来的不便请谅解.",
//        "第二张", "第三张", "第四张"};
//延时跳转对象
    private Handler handler;

    private LayoutInflater inflater;
    private int maxItem = 0;

    private Runnable r = new Runnable() {
        @Override
        public void run() {
            int index = viewPager.getCurrentItem();
            viewPager.setCurrentItem(++index);
            //延时两秒改变下标
            handler.postDelayed(r, 3000);
        }
    };
    private LinearLayout v;




    private ImageView foodIv,showIv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_food, null);
        return v;


    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        pagerAdapter = new AfficheViewPagerAdapter();
        viewPager.setAdapter(pagerAdapter);


        foodIv = (ImageView)view.findViewById(R.id.foodIv);
        showIv = (ImageView)view.findViewById(R.id.showIv);

        foodIv.setOnClickListener(this);
        showIv.setOnClickListener(this);

        //遍历数组
        for (int i = 0; i < image.length; i ++){
            Map<String, Object> map = new HashMap<>();
            map.put("image", image[i]);

            list.add(map);
        }
        maxItem = list.size();//设置最大长度
        handler = new Handler();//初始化适配器
        handler.postDelayed(r, 3000);//延时改变下标

        pagerAdapter.addData(list);//添加到适配器


        v = (LinearLayout) view.findViewById(R.id.indicator);

        viewPager.addOnPageChangeListener(this);


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initIndicator();
    }

    private void initIndicator() {
        ImageView imageView;
        //线性水平布局 负责动态调整导航图标

        //创建小圆点
        for (int i = 0; i < image.length; i ++ ) {
            imageView = new ImageView(getActivity());
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
            layoutParams.setMargins(20, 30, 20, 30);
            imageView.setLayoutParams(layoutParams);
            image_small [i] = imageView;

            if (i == 0){//初始化 第一个为选中状态
                image_small[i].setBackgroundResource(R.mipmap.iconfont_yuandian_2);
            }else{
                image_small[i].setBackgroundResource(R.mipmap.iconfont_yuandian_3);
            }
            v.addView(image_small[i]);//将小圆点数组 添加到线性布局中
        }
    }



    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    public void onPageSelected(int position) {
        //遍历数组 设置小圆点的图片
        for (int i = 0; i < image_small.length; i ++){
            image_small[(position%list.size())].setBackgroundResource(R.mipmap.iconfont_yuandian_2);
            //判断 如果当前位置不等于i 则设置成另一个图片
            if ((position%list.size()) != i){
                image_small[i].setBackgroundResource(R.mipmap.iconfont_yuandian_3);
            }
        }



    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.foodIv:
                Intent houseIntent = new Intent();
                houseIntent.setClass(getActivity(), FoodHouseActivity.class);
                startActivity(houseIntent);
                break;
            case R.id.showIv:
                Intent showIntent = new Intent();
                showIntent.setClass(getActivity(), FoodShowActivity.class);
                startActivity(showIntent);
                break;

        }

    }

}
